Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented JSON distributed trace converter #335

Merged
merged 4 commits into from
Jul 1, 2021
Merged

implemented JSON distributed trace converter #335

merged 4 commits into from
Jul 1, 2021

Conversation

nr-swilloughby
Copy link
Contributor

Summary

When making calls to a transaction's AcceptDistributedTraceHeaders() you are required to pass a valid http.Header value holding the distributed trace headers you want the current transaction to use. However, sometimes this data arrives in a text form such as a JSON string.

This requires the programmer to perform multiple steps to build up the http.Header value, which can be prone to errors. I propose a convenience function that constructs this value in one step.

Changes

This PR adds two functions. DistributedTraceHeadersFromJson() converts a JSON representation of the trace headers to an http.Header object.

AcceptDistributedTraceHeadersFromJson() does the above plus calls AcceptDistributedTraceHeaders() on the result for you.

Details

Given a JSON string such as:

{
   "traceparent": "frob",
   "tracestate": "blorfl",
   "newrelic": "xyzzy"
}

it will output the http.Header value with three header keys (traceparent, tracestate, and newrelic) with corresponding values as provided in the JSON string.

It will also appropriately handle the case where multiple values are provided in the JSON string for a single key.

An error will be returned if it was unable to parse the JSON string or ran into other difficulties.

@RichVanderwal
Copy link
Contributor

Looks like we're getting errors on the 1.15.x test run:

Error: transaction.go:274:1: comment on exported method Transaction.AcceptDistributedTraceHeadersFromJson should be of the form "AcceptDistributedTraceHeadersFromJson ..."
Error: transaction.go:281:25: method AcceptDistributedTraceHeadersFromJson should be AcceptDistributedTraceHeadersFromJSON
Error: transaction.go:290:1: comment on exported function DistributedTraceHeadersFromJson should be of the form "DistributedTraceHeadersFromJson ..."
Error: transaction.go:305:6: func DistributedTraceHeadersFromJson should be DistributedTraceHeadersFromJSON
Error: transaction.go:328:24: error strings should not be capitalized or end with punctuation or a newline
Error: transaction.go:333:22: error strings should not be capitalized or end with punctuation or a newline
Error: transaction.go:338:20: error strings should not be capitalized or end with punctuation or a newline

v3/go.mod Outdated
Comment on lines 6 to 7
github.com/golang/protobuf v1.4.3
google.golang.org/grpc v1.39.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're requiring new versions of these libraries, will this affect our customers in any way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can move the version back. That was automatically bumped by my go runtime, not because I purposefully upgraded it.

{"{}", http.Header{}, false},
{" invalid ", http.Header{}, true},
{`"foo"`, http.Header{}, true},
{`{"foo": "bar"}`, map[string][]string{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this map[string][]string rather than just another http.Header? I've changed them back to http.Header and all seems to work fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http.Header is more correct to use, even though they're (currently) equivalent.

"beta",
"gamma"
]
}`, map[string][]string{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here with http.Header vs. map[string][]string

Comment on lines 299 to 300
// This is a convenience function provided for cases where you receive the trace header data
// already as a JSON string and want to avoid manually converting that to an http.Header.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we want to add that this is likely when working with trace headers with different language agents? Maybe that would be too wordy. I just want to make sure somebody like Jo Ann would see this and use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a bad idea. I don't mind leaning to more verbosity when it comes to documentation. You never know when that extra sentence or paragraph was just what someone was looking for.

Copy link
Contributor

@RichVanderwal RichVanderwal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@nr-swilloughby nr-swilloughby merged commit 1945953 into newrelic:develop Jul 1, 2021
@nr-swilloughby nr-swilloughby deleted the 315_json_header_builder branch July 1, 2021 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants